package com.oa.modules.approvalsec1.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 流程项
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-12-10 10:45:00
 */
@Mapper
public interface ProcessItemDao extends BaseMapper<ProcessItemEntity> {


    /**
     * 查询总条数
     *
     * @author jiangwei
     * @email 785297226@qq.com
     * @date 2021-12-10 10:45:00
     * @Description 查询分页数据
     */
    @Select("<script>SELECT COUNT( 1 ) FROM (" +
            "select t1.* from xxx t1  " +
            "where " +
            "<if test = 'params != null '> "+
            "t1.`STATUS` = #{params.status} " +
            ") </if> TOTAL</script>")
    long findPage(@Param("params") Map<String, Object> params);

    /**
     * 查询分页数据
     *
     * @author jiangwei
     * @email 785297226@qq.com
     * @date 2021-12-10 10:45:00
     * @Description 查询分页数据
     */
    @Select("<script>select t1.* from xxx t1 " +
            "where " +
            "t1.`STATUS` = #{params.status} " +
            "ORDER BY t1.CREATE_DATE desc</script>")
    List<ProcessItemEntity> findRecords(IPage<ProcessItemEntity> page, @Param("params") Map<String, Object> params);

    List<ProcessItemEntity> getProcessItem(Integer projectId);

    void saveProcessItem(Map<String,Object> map);

    void setIsEnabled(@Param("scheduleId") Integer scheduleId,@Param("processId") Integer processId);


    /**
     * 根据流程id查询流程下的所有节点
     *
     * @author jiangwei
     * @email 785297226@qq.com
     * @date 2021-12-10 10:45:00
     * @Description 查询分页数据
     */
//    @Select("<script>select distinct p.*,ps.name,ps.parent_id " +
//            "from process_item p " +
//            "left join project_file pf on p.id = pf.item_id " +
//            "left join project_schedule ps on p.schedule_id = ps.id " +
//            "where p.process_id = ${processId} and ps.parent_id >= 0 " +
//            "ORDER BY p.schedule_id</script>")

    @Select("<script>" +
            "select p.*,ps.name,ps.parent_id,pj.type listType from process_item p "+
            "left join project_schedule ps on "+
            "p.schedule_id = ps.id "+
            "left join project pj on pj.process_id = p.process_id "+
            "where p.process_id = ${processId} and p.is_enabled=1 and pj.id = #{id}  ORDER BY p.sort"+
            "</script>")
    List<ProcessItemEntity> queryAllItem(@Param("processId") Integer processId,@Param("id") Integer id);

}
